<?php
Class Controller_Finance_Business extends Controller_Abstract
{
	function actionPurchaseOrderList()
	{
		$this->_view['title'] = '采购入库单列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$this->_view['warehouse_group'] = Warehouse::getTreeWarehouse();
		$this->_view['warehouse'] = Warehouse::getActiveWarehouse();
		$url_arr = Helper_BSS_Normal::buildCondition();
		$url_arr['type'] = Q::ini('custom_flag/purchase_order_type/order/value');
		if (isset($_GET['search_object_sel']) && ! empty($_GET['begin_storage_audit_time']) && ! empty($_GET['end_storage_audit_time']))
		{
			$purchase_order_where = 'type=1 and storage_audit_time>\'' . $_GET['begin_storage_audit_time'] . '\' and storage_audit_time<\'' . $_GET['end_storage_audit_time'] . ' 23:59:59\'';
			$purchase_order = QDB::getConn()->execute('select distinct id from purchase_order where ' . $purchase_order_where)->fetchCol();
			$purchase_order_item_where = 'audit_time>\'' . $_GET['begin_storage_audit_time'] . '\' and audit_time<\'' . $_GET['end_storage_audit_time'] . ' 23:59:59\'';
			$purchase_order_item = QDB::getConn()->execute('select distinct purchase_order_id from purchase_order_item where ' . $purchase_order_item_where)->fetchCol();
			$cross_where = '(audit_time<\'' . $_GET['begin_storage_audit_time'] . '\' or audit_time>\'' . $_GET['end_storage_audit_time'] . ' 23:59:59\') and purchase_order_id in (' . implode(',', $purchase_order_item) . ')';
			$cross = QDB::getConn()->execute('select distinct purchase_order_id from purchase_order_item where ' . $cross_where)->fetchCol();
			$search_id = ($_GET['search'] == 1 ? $purchase_order : ($_GET['search'] == 2 ? $purchase_order_item : $cross));
			$result = Purchase_Order::search(array('id' => $search_id), $cp, $ps);
		}
		else
		{
			$result = Purchase_Order::search($url_arr, $cp, $ps);
		}
		if ($result['ack'] == SUCCESS)
		{
			$this->_view['purchase_order'] = $result['data'];
			$page = new Helper_BSS_Page($result['pagination'], url('Finance::Business/PurchaseorderList', $url_arr));
			$this->_view['page'] = $page->getPage();
			if (isset($_GET['search_object_sel']) && ! empty($_GET['begin_storage_audit_time']) && ! empty($_GET['end_storage_audit_time']))
			{
				$this->_view['item_amount'] = QDB::getConn()->execute('select sum(actual_price*actual_quantity) from purchase_order_item where ' . $purchase_order_item_where)->fetchOne();
				$this->_view['amount'] = QDB::getConn()->execute('select sum(actual_amount) from purchase_order where ' . $purchase_order_where)->fetchOne();
				$this->_view['url_arr_json'] = json_encode(array('id' => $search_id));
			}
			else
			{
				$where = Purchase_Order::buildSearchWhere($url_arr);
				$amount = Purchase_Order::find($where)
					->joinLeft('user', '', 'user.id=purchase_order.purchase_user_id')
					->getSum('actual_amount');
				$this->_view['amount'] = $amount;
				$this->_view['url_arr_json'] = json_encode($url_arr);
			}
		}
	}
	
	function actionPurchaseDrawbackList()
	{
		$this->_view['title'] = '采购退货单列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$this->_view['warehouse'] = Warehouse::getActiveWarehouse();
		$url_arr = Helper_BSS_Normal::buildCondition();
		$url_arr['type'] = Q::ini('custom_flag/purchase_order_type/drawback/value');
		$this->_view['url_json'] = json_encode($url_arr);
		$result = Purchase_Order::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$purchase_order = $result['data'];
			$this->_view['purchase_order'] = $purchase_order;
			$page = new Helper_BSS_Page($result['pagination'], url('Finance::Business/PurchaseDrawbackList', $url_arr));
			$this->_view['page'] = $page->getPage();
			$where = Purchase_Order::buildSearchWhere($url_arr);
			$amount = Purchase_Order::find($where)->getSum('actual_amount');
			$this->_view['amount'] = $amount;
		}
	}
	
	function actionPurchaseDrawbackDownload()
	{
		$this->_view['title'] = '请求下载采购退货表';
		if (! empty($_GET['url_json']))
		{
			$url_arr = json_decode($_GET['url_json'], true);
			$where = Purchase_Order::buildSearchWhere($url_arr);
			$purchase_order = QDB::getConn()->execute('select purchase_order.id, purchase_order.storage_audit_time, purchase_order.number, purchase_order.actual_amount, warehouse.name as warehouse_name, sum(purchase_order_item.actual_quantity) as actual_quantity, purchase_order.notes, user.name as purchase_user_name from purchase_order left join purchase_order_item on purchase_order_item.purchase_order_id=purchase_order.id left join user on user.id=purchase_order.purchase_user_id left join warehouse on warehouse.id=purchase_order.warehouse_id where ' . $where . ' group by purchase_order.id')->fetchAll();
			include Q::ini('custom_system/lib_dir').'PHPExcel.php';
			$phpexcel = new PHPExcel();
			$phpexcel->getProperties()->setCreator(Q::ini('custom_system/system_name') . Q::ini('custom_system/version'));
			$phpexcel->getProperties()->setLastModifiedBy(Q::ini('custom_system/system_name') . Q::ini('custom_system/version'));
			$phpexcel->getProperties()->setTitle('采购退货报表');
			$phpexcel->getProperties()->setSubject('采购退货报表');
			$phpexcel->getProperties()->setDescription('采购退货报表-运营支撑系统');
			$phpexcel->getProperties()->setKeywords('采购退货报表');
			$phpexcel->getProperties()->setCategory('采购退货报表');
			$phpexcel->setActiveSheetIndex(0);
			$phpexcel->getActiveSheet()->setTitle('采购退货报表');
			$phpexcel->getActiveSheet()->getStyle('A1:G1')->getFont()->setName('Candara');
			$phpexcel->getActiveSheet()->getStyle('A1:G1')->getFont()->setSize(15);
			$phpexcel->getActiveSheet()->getStyle('A1:G1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->setCellValue('A1', "仓库审核日期")->getColumnDimension('A')->setAutoSize(true);
			$phpexcel->getActiveSheet()->setCellValue('B1', "经手人")->getColumnDimension('B')->setWidth(15);
			$phpexcel->getActiveSheet()->setCellValue('C1', "仓库")->getColumnDimension('C')->setAutoSize(true);
			$phpexcel->getActiveSheet()->setCellValue('D1', "单号")->getColumnDimension('D')->setAutoSize(true);
			$phpexcel->getActiveSheet()->setCellValue('E1', "金额")->getColumnDimension('E')->setWidth(20);
			$phpexcel->getActiveSheet()->setCellValue('F1', "数量")->getColumnDimension('F')->setWidth(20);
			$phpexcel->getActiveSheet()->setCellValue('G1', "备注")->getColumnDimension('G')->setWidth(20);
			$i = 2;
			foreach ($purchase_order as $po)
			{
				$phpexcel->getActiveSheet()->getStyle("A{$i}:G{$i}")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
				$phpexcel->getActiveSheet()->getStyle("A{$i}:G{$i}")->getFont()->setSize(12);
				$phpexcel->getActiveSheet()->setCellValue('A' . $i, $po['storage_audit_time']);
				$phpexcel->getActiveSheet()->setCellValue('B' . $i, $po['purchase_user_name']);
				$phpexcel->getActiveSheet()->setCellValue('C' . $i, $po['warehouse_name']);
				$phpexcel->getActiveSheet()->setCellValue('D' . $i, $po['number']);
				$phpexcel->getActiveSheet()->setCellValue('E' . $i, $po['actual_amount']);
				$phpexcel->getActiveSheet()->setCellValue('F' . $i, $po['actual_quantity']);
				$phpexcel->getActiveSheet()->setCellValue('G' . $i, $po['notes']);
				$i++;
				$purchase_order_item = Purchase_Order_Item::find('purchase_order_item.purchase_order_id=?', $po['id'])
					->joinLeft('product_basic', 'product_basic.number as product_basic_number, last_purchase_price', 'product_basic.id=purchase_order_item.product_basic_id')
					->asArray()->getAll();
				$phpexcel->getActiveSheet()->getStyle("D{$i}:G{$i}")->getFont()->setName("Candara");
				$phpexcel->getActiveSheet()->getStyle("D{$i}:G{$i}")->getFont()->setSize(11);
				$phpexcel->getActiveSheet()->getStyle("D{$i}:G{$i}")->getFont()->setBold(true);
				$phpexcel->getActiveSheet()->setCellValue('D' . $i, '产品编号');
				$phpexcel->getActiveSheet()->setCellValue('E' . $i, '实际单价');
				$phpexcel->getActiveSheet()->setCellValue('F' . $i, '实际数量');
				$phpexcel->getActiveSheet()->setCellValue('G' . $i, '最近采购价');
				$i++;
				foreach ($purchase_order_item as $poi)
				{
					$phpexcel->getActiveSheet()->getStyle("D{$i}:G{$i}")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
					$phpexcel->getActiveSheet()->getCell('D' . $i)->setValueExplicit($poi['product_basic_number'], PHPExcel_Cell_DataType::TYPE_STRING);
					$phpexcel->getActiveSheet()->setCellValue('E' . $i, $poi['actual_price']);
					$phpexcel->getActiveSheet()->setCellValue('F' . $i, $poi['actual_quantity']);
					$phpexcel->getActiveSheet()->setCellValue('G' . $i, $poi['last_purchase_price']);
					$i++;
				}
			}
			$objWriter = new PHPExcel_Writer_Excel5($phpexcel);
			$file_name = 'purchasedrawback-' . date('Y-m-d', CURRENT_TIMESTAMP) . '-' . CURRENT_USER_NAME . ".xls";
			$objWriter->save(Q::ini('custom_system/export_dir') . $file_name);
			echo json_encode(array('ack' => 'success', 'url' => Q::ini('custom_system/export_url') . $file_name));
		}
		else
		{
			echo json_encode(array('ack' => 'failure'));
		}
	}
	
	function actionPurchaseDetail()
	{
		$this->_view['title'] = '采购（退货）单详细';
		$url_arr = Helper_BSS_Normal::buildCondition();
		if ($url_arr)
		{
			$purchase_order = Purchase_Order::find('purchase_order.id =?', $url_arr['id'])
				->joinLeft('warehouse', 'name as warehouse_name', 'warehouse.id=purchase_order.warehouse_id')
				->joinLeft('user', 'name as purchase_user_name', 'user.id = purchase_order.purchase_user_id')
				->asArray()->getOne();
			$item = Purchase_Order_Item::find('purchase_order_item.purchase_order_id =?', $url_arr['id'])
				->joinLeft('purchase_order', 'purchase_order.storage_audit_time', 'purchase_order.id=purchase_order_item.purchase_order_id')
				->joinLeft('product_basic', 'number as product_number, name', 'product_basic.id = purchase_order_item.product_basic_id')
				->joinLeft('product_provider as a', 'name as budget_provider_name', 'a.id = purchase_order_item.budget_provider_id')
				->joinLeft('product_provider as b', 'name as actual_provider_name', 'b.id = purchase_order_item.actual_provider_id')
				->joinLeft('product_warehouse', 'product_warehouse.purchase_budget_quantity, product_warehouse.purchase_quantity, product_warehouse.purchase_status, product_warehouse.sales_status, product_warehouse.actual_quantity as warehouse_actual_quantity, product_warehouse.sold_quantity, product_warehouse.safe_quantity, product_warehouse.warning_quantity', 'product_warehouse.product_basic_id=product_basic.id and product_warehouse.warehouse_id=' . $purchase_order['warehouse_id'])
				->asArray()->getAll();
			Helper_BSS_Normal::getProductImgUrl($item, 'product_number');
			$this->_view['purchase_order'] = $purchase_order;
			$this->_view['item'] = $item;
		}
		else
		{
			return $this->_redirect(url('Purchase::Order/OrderList'));
		}
	}
	
	function actionPurchaseLossAuditlist()
	{
		$this->_view['title'] = '采购损耗列表';
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$url_arr['quantity'] = isset($url_arr['quantity']) ? isset($url_arr['quantity']) : 1;
		$result = Purchase_Loss::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Page($result['pagination'], url('Finance::Business/PurchaseLossAuditList', $url_arr));
			$this->_view['page'] = $page->getPage();
			$purchase_loss = $result['data'];
			Helper_BSS_Normal::getProductImgUrl($purchase_loss);
			$this->_view['purchase_loss'] = $purchase_loss;
			$where = Purchase_Loss::buildSearchWhere($url_arr);
			$this->_view['amount'] = Purchase_Loss::find($where)->getSum('price*quantity');
		}
	}
	
	function actionPurchaseLossAudit()
	{
		$this->_view['title'] = '采购损耗审核（操作）';
		$ids = $_REQUEST['ids'];
		if ($ids)
		{
			$ids = explode(',', $ids);
			$result = Purchase_Loss::financeAudit($ids);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('审核成功', '返回列表', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('审核失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionOtherInList()
	{
		$this->_view['title'] = '其他入库单列表';
		$warehouse = Warehouse::find()->asArray()->getAll();
		$this->_view['warehouse'] = $warehouse;
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 0;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$url_arr['type'] = Q::ini('custom_flag/other_io_type/in/value');
		$result = Other_IO::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$other_io = $result['data'];
			Other_IO::calculateCostAmount($other_io);
			$where = Other_IO::buildSearchWhere($url_arr);
			$amount = Other_IO_Item::find($where)
				->joinLeft('other_io', '', 'other_io.id=other_io_item.other_io_id')
				->getSum('cost_price*quantity');
			$this->_view['amount'] = $amount;
			$page = new Helper_BSS_Page($result['pagination'], url('Finance::Business/OtherInList', $url_arr));
			$this->_view['page'] = $page->getPage();
			$this->_view['other_io'] = $other_io;
		}
	}
	
	function actionOtherIOAudit()
	{
		$this->_view['title'] = '其他出入库单审核';
		$ids = $_REQUEST['ids'];
		if ($ids)
		{
			$ids = explode(',', $ids);
			$result = Other_IO::financeAudit($ids);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('审核成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('审核失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionOtherOutList()
	{
		$this->_view['title'] = '其他出库单列表';
		$warehouse = Warehouse::find()->asArray()->getAll();
		$this->_view['warehouse'] = $warehouse;
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 0;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$url_arr['type'] = Q::ini('custom_flag/other_io_type/out/value');
		$result = Other_IO::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$other_io = $result['data'];
			Other_IO::calculateCostAmount($other_io);
			$where = Other_IO::buildSearchWhere($url_arr);
			$amount = Other_IO_Item::find($where)
				->joinLeft('other_io', '', 'other_io.id=other_io_item.other_io_id')
				->getSum('cost_price*quantity');
			$this->_view['amount'] = $amount;
			$page = new Helper_BSS_Page($result['pagination'], url('Finance::Business/OtherOutList', $url_arr));
			$this->_view['other_io'] = $other_io;
			$this->_view['page'] = $page->getPage();;
		}
	}
	
	function actionOtherIODetail()
	{
		$this->_view['title'] = '其他出入库产品信息详细';
		$id = $_GET['id'];
		if ($id)
		{
			$other_io = Other_IO::find('other_io.id=?', $id)
				->joinLeft('user', 'name as user_name', 'user.id=other_io.user_id')
				->asArray()->getOne();
			$item = Other_IO_Item::find('other_io_item.other_io_id=?', $id)
				->joinLeft('product_basic', 'name, number', 'product_basic.id=other_io_item.product_basic_id')
				->asArray()->getAll();
			$this->_view['other_io'] = $other_io;
			$this->_view['item'] = $item;
		}
	}
	
	function actionPurchaseOrderAudit()
	{
		$this->_view['title'] = '采购入库单审核';
		$ids = explode(',', $_GET['ids']);
		if ($ids)
		{
			$result = Purchase_Order::auditFinance($ids);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('审核成功', '返回列表', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('审核失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionUpdatePurchaseOrderActualAmount()
	{
		$this->_view['title'] = '更新采购单实际总金额';
		if (! empty($_GET['ids']))
		{
			$purchase_order = Purchase_Order::find('id=? and type=? and finance_audit=? and (status=? or status=?)', $_GET['ids'], Q::ini('custom_flag/purchase_order_type/order/value'), Q::ini('custom_flag/purchase_order_finance_audit/unaudit/value'), Q::ini('custom_flag/purchase_order_status/storaged/value'), Q::ini('custom_flag/purchase_order_status/send/value'))->asArray()->getOne();
			if ($purchase_order)
			{
				Purchase_Order::updateOrder($purchase_order['id']);
				return $this->_redirectMessage('更新订单总金额成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('重新计算总金额失败', '没有符合采购单，请检查订单状态及审核情况！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
		else
		{
			return $this->_redirect($_SERVER['HTTP_REFERER']);
		}
	}
	
	function actionPurchaseDrawbackAudit()
	{
		$this->_view['title'] = '采购退货单审核';
		$ids = explode(',', $_GET['ids']);
		if ($ids)
		{
			$result = Purchase_Order::auditDrawbackFinance($ids);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('审核成功', '返回列表', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('审核失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionRefundList()
	{
		$this->_view['title'] = '退款单列表';
		$shop = Shop::getActiveShop();
		$shop_group = array();
		foreach ($shop as $s)
		{
			$shop_group[$s['platform']][] = $s;
		}
		$this->_view['shop_group'] = $shop_group;
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$this->_view['url_arr'] = $url_arr;
		$result = Sales_Refund::Search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$sales_refund = $result['data'];
			$page = new Helper_BSS_Page($result['pagination'], url('Finance::Business/RefundList', $url_arr));
			$this->_view['page'] = $page->getPage();
			Sales_Order_Notes::getOrderNotes($sales_refund);
			$this->_view['sales_refund'] = $sales_refund;
			$currency = Currency::find()->asArray()->getAll();
			foreach ($currency as $c)
			{
				$currency_code[$c['code']] = 0;
				$currency_rate[$c['code']] = $c['rate'];
			}
			$refund = Sales_Refund::search($url_arr, 0, 0);
			foreach ($refund['data'] as $r)
			{
				if (array_key_exists($r['currency_code'], $currency_code))
				{
					$currency_code[$r['currency_code']] += $r['refund_amount'] * $r['type'];
				}
			}
			$RMB = 0;
			foreach ($currency_code as $key => $cc)
			{
				if ($cc && array_key_exists($key, $currency_rate))
				{
					$RMB += $cc * $currency_rate[$key];
				}
			}
			$this->_view['RMB'] = $RMB;
			$this->_view['currency_code'] = $currency_code;
		}
	}
	
	function actionRefundDownload()
	{
		$this->_view['title'] = '请求下载退款表';
		set_time_limit(300);
		$url_arr = json_decode($_REQUEST['url_arr_json'], true);
		if (isset($url_arr['is_search']))
		{
			$result = Sales_Refund::search($url_arr, 0, 0);
			//生成excel表
			include Q::ini('custom_system/lib_dir').'PHPExcel.php';
			$phpexcel = new PHPExcel();
			$phpexcel->getProperties()->setCreator(Q::ini('custom_system/system_name') . Q::ini('custom_system/version'));
			$phpexcel->getProperties()->setLastModifiedBy(Q::ini('custom_system/system_name') . Q::ini('custom_system/version'));
			$phpexcel->getProperties()->setTitle("退款表");
			$phpexcel->getProperties()->setSubject("退款表");
			$phpexcel->getProperties()->setDescription("退款表-运营支撑系统");
			$phpexcel->getProperties()->setKeywords("退款表");
			$phpexcel->getProperties()->setCategory("退款表");
			$phpexcel->setActiveSheetIndex(0);
			$phpexcel->getActiveSheet()->setTitle("退款表");
			$phpexcel->getActiveSheet()->setCellValue('A1', "ID");
			$phpexcel->getActiveSheet()->setCellValue('B1', "退款日期");
			$phpexcel->getActiveSheet()->setCellvalue('C1', '网站/店铺');
			$phpexcel->getActiveSheet()->setCellValue('D1', "退款单号");
			$phpexcel->getActiveSheet()->setCellValue('E1', "币种");
			$phpexcel->getActiveSheet()->setCellValue('F1', "金额");
			$phpexcel->getActiveSheet()->setCellValue('G1', "退款方式");
			$phpexcel->getActiveSheet()->setCellValue('H1', "退款账号");
			$phpexcel->getActiveSheet()->setCellValue('I1', "收款交易号");
			$phpexcel->getActiveSheet()->setCellValue('J1', "退款交易号");
			$phpexcel->getActiveSheet()->setCellValue('K1', "退款原因");
			$phpexcel->getActiveSheet()->setCellValue('L1', "备注");
			$phpexcel->getActiveSheet()->setCellValue('M1', "发货状态");
			$phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('E1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('F1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('G1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('H1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('I1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('J1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('K1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('L1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('J1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('K1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('L1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('M1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$i = 2;
			foreach ($result['data'] as $d)
			{
				$phpexcel->getActiveSheet()->getCell('A'.$i)->setValueExplicit($d['id'], PHPExcel_Cell_DataType::TYPE_STRING);
				$phpexcel->getActiveSheet()->setCellValue('B'.$i, $d['refund_time']);
				$phpexcel->getActiveSheet()->setCellValue('C'.$i, $d['shop_name']);
				$phpexcel->getActiveSheet()->setCellValue('D'.$i, $d['number']);
				$phpexcel->getActiveSheet()->setCellValue('E'.$i, $d['currency_code']);
				$phpexcel->getActiveSheet()->setCellValue('F'.$i, sprintf('%.2f', $d['refund_amount'] * $d['type']));
				$phpexcel->getActiveSheet()->setCellValue('G'.$i, Helper_BSS_Normal::getIniOne('sales_order_payment_method', 'value', $d['refund_method'], 'name'));
				$phpexcel->getActiveSheet()->setCellValue('H'.$i, $d['refund_account']);
				$phpexcel->getActiveSheet()->setCellValue('I'.$i, isset($d['receive_transaction_id']) ? $d['receive_transaction_id'] : '');
				$phpexcel->getActiveSheet()->setCellValue('J'.$i, isset($d['refund_transaction_id']) ? $d['refund_transaction_id'] : '');
				$phpexcel->getActiveSheet()->setCellValue('K'.$i, Helper_BSS_Normal::getIniOne('sales_refund_reason', 'value', $d['reason'], 'name'));
				$phpexcel->getActiveSheet()->setCellValue('L'.$i, $d['notes']);
				$phpexcel->getActiveSheet()->setCellValue('M'.$i, Helper_BSS_Normal::getIniOne('sales_order_shipping_status', 'value', $d['shipping_status'], 'name'));
				$i++;
			}
			$writer = new PHPExcel_Writer_Excel5($phpexcel);
			$file_name = 'refund-' . date('YmdHis', CURRENT_TIMESTAMP) . '-' . CURRENT_USER_NAME . ".xls";
			$writer->save(Q::ini('custom_system/export_dir').$file_name);
			echo json_encode(array(
					'ack' => 'success',
					'url' => Q::ini('custom_system/export_url') . $file_name
			)
			);
		}
		else
		{
			echo json_encode(array('ack'=>'failure'));
		}
	}
	
	function actionRefundDetect()
	{
		$this->_view['title'] = '退款检测对比（交易号和金额）';
		if (isset($_POST['refund_info']) && $_POST['refund_info'])
		{
			$excess_refund = $wrong_refund_amount = $wrong_refund_transaction_id = '';
 			$exists_refund_transactino_id = array();
			$refund_record = explode("\n", $_POST['refund_info']);
			foreach ($refund_record as $rr)
			{
				$refund_info[] = explode(',', $rr); 
			}
			foreach ($refund_info as $ri)
			{
				$condition = unserialize(isset($_POST['url_arr']) ? $_POST['url_arr'] : '');
				$where = Sales_Refund::buildSearchWhere($condition);
				$check_refund_transaction_id = Sales_Refund::find($where . ' and sales_refund.refund_transaction_id=?', trim($ri[0]))
					->joinLeft('sales_order', '', 'sales_order.id=sales_refund.sales_order_id')
					->asArray()->getOne();
				if ($check_refund_transaction_id['id'])
				{
					$check_refund_amount = Sales_Refund::find('sales_refund.id=?', $check_refund_transaction_id['id'])->asArray()->getOne();
					if ($check_refund_amount['refund_amount'] * $check_refund_amount['type'] != $ri[1])
					{
						$wrong_refund_amount .= $check_refund_amount['refund_transaction_id'] . '：系统金额：' . $check_refund_amount['refund_amount'] * $check_refund_amount['type'] . '—对比金额：' . $ri[1] . '<br />';
					}
					$exists_refund_transactino_id[$check_refund_amount['refund_transaction_id']] = $check_refund_transaction_id['refund_transaction_id'];
				}
				else
				{
					$wrong_refund_transaction_id .= $ri[0] . ' - ' . $ri[1] . ' - ' . $ri[2] . '<br />';
				}
			}
			$refund_search_all = Sales_Refund::find($where)
				->joinLeft('sales_order', '', 'sales_order.id=sales_refund.sales_order_id')
				->asArray()->getAll();
			if ($refund_search_all)
			{
				foreach ($refund_search_all as $r)
				{
					if (! array_key_exists($r['refund_transaction_id'], $exists_refund_transactino_id))
					{
						$excess_refund .= $r['refund_transaction_id'] . '—金额：' . $r['refund_amount'] * $r['type'] . '</br>';
					}
				}
			}
			if (empty($wrong_refund_amount) && empty($wrong_refund_transaction_id) && empty($excess_refund))
			{
				$this->_view['message'] = '<span class="red">检测对比成功，实际填写的退款交易号和金额与系统一致！</span>';
			}
			else
			{
				if (! empty($wrong_refund_amount))
				{
					$this->_view['wrong_refund_amount'] = '<span class="red">金额错误：<br/>' . $wrong_refund_amount . '</span><br />';
				}
				if (! empty($wrong_refund_transaction_id))
				{
					$this->_view['wrong_refund_transaction_id'] = '<span class="red">系统不存在交易号：<br/>' . $wrong_refund_transaction_id . '</span><br />';
				}
				if (! empty($excess_refund))
				{
					$this->_view['excess_refund'] = '<span>系统还存在退款交易号：<br/>' . $excess_refund . '</span>';
				}
			}
		}
	}
	
	function actionRefundBulkEdit()
	{
		$this->_view['title'] = '退款单批量修改';
		if ($this->_context->isPOST())
		{
			$result = Sales_Refund::editSalesRefundInfo($_POST);
			if ($result['ack'] == SUCCESS)
			{
				$this->_view['success'] = '修改成功！';
			}
			else
			{
				$this->_view['message'] = $result['message'];
			}
		}
	}

	function actionRefundEdit()
	{
		$this->_view['title'] = '退款单编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		if ($id)
		{
			$sales_refund = Sales_Refund::find('id=?', $id)->asArray()->getOne();
			$this->_view['sales_refund'] = $sales_refund;
		}
	}
	
	function actionRefundEditSave()
	{
		$this->_view['title'] = '退款单编辑保存';
		if ($_POST['id'])
		{
			$sales_refund = array(
					'id' => $_POST['id'],
					'type' => $_POST['type'],
					'refund_amount' => $_POST['refund_amount'],
					'refund_account' => $_POST['refund_account'],
					'receive_transaction_id' => trim($_POST['receive_transaction_id']),
					'refund_method' => $_POST['refund_method'],
					'categories_name' => trim($_POST['categories_name']),
					'reason' => isset($_POST['reason']) ? $_POST['reason'] : '',
					'notes' => trim($_POST['notes'])
			);
			$result = Sales_Refund::edit($sales_refund);
			if ($result['ack'] == SUCCESS)
			{
				echo '保存成功';exit;
			}
			else
			{
				echo $result['message'];exit;
			}
		}
	}
	
	function actionRefundDelete()
	{
		$this->_view['title'] = '退款单删除';
		$result = Sales_refund::delete($_GET['id']);
		if ($result['ack'] == SUCCESS)
		{
			return $this->_redirectMessage('删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
		}
		else
		{
			return $this->_redirectMessage('删除失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
		}
	}
	
	function actionRefundAudit()
	{
		$this->_view['title'] = '退款单审核';
		if ($this->_context->isPOST())
		{
			$refund = array();
			$ids = explode(',', $_POST['ids']);
			$refund['refund_transaction_id'] = isset($_POST['refund_transaction_id']) ? trim($_POST['refund_transaction_id']) : '';
			$refund['refund_time'] = isset($_POST['refund_time']) ? $_POST['refund_time'] : CURRENT_DATETIME;
			$result = Sales_Refund::auditRefundFinance($ids, $refund);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('退款审核成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('部分退款审核失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
		else
		{
			return $this->_redirect(url('Finance::Business/RefundList'));
		}
	}
	
	function actionRefundInfo()
	{
		$this->_view['title'] = '退款详细信息';
		if ($_GET['id'])
		{
			$refund = Sales_Refund::find('id=?', $_GET['id'])
				->asArray()->getOne();
			$sales_order_id = $refund['sales_order_id'];
			$refund['item'] = Sales_Refund::find('sales_order_id=?', $sales_order_id)->asArray()->getAll();
			$sales_order = Sales_Order::find('id=?', $sales_order_id)->asArray()->getOne();
			$shop = Shop::find('id=?', $sales_order['shop_id'])->asArray()->getOne();
			$this->_view['shop'] = $shop;
			$this->_view['sales_order'] = $sales_order;
			$this->_view['refund'] = $refund['item'];
		}
	}
	
	function actionShippingFeeList()
	{
		$this->_view['title'] = '订单运费列表';
		$this->_view['warehouse_group'] = Warehouse::getTreeWarehouse();
		$shop = Shop_User_Mapping::getTreeShopByPlatform();
		$this->_view['shop_group'] = $shop;
		$warehouse = Warehouse::find()->asArray()->getAll();
		$this->_view['shipping_method'] = Shipping_Method::getAllTreeShippingMethod();
		if (isset($_GET['is_search']))
		{
			$cp = isset($_GET['cp']) ? $_GET['cp'] : 0;
			$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
			$url_arr = Helper_BSS_Normal::buildCondition();
			$url_arr['shipping_status'] = Q::ini('custom_flag/sales_order_shipping_status/shipped/value');
			$this->_view['url_arr'] = json_encode($url_arr);
			$result = Sales_Order::search($url_arr, $cp, $ps);
			if ($result['ack'] == SUCCESS)
			{
				$sales_order = $result['data'];
				$page = new Helper_BSS_Page($result['pagination'], url('Finance::Business/ShippingFeeList', $url_arr));
				$this->_view['page'] = $page->getPage();
				$this->_view['sales_order'] = $sales_order;
				$where = Sales_Order::buildSearchWhere($url_arr);
				$postage = QDB::getConn()->execute('select sum(weight) as weight, sum(sales_order.postage) as postage_amount, sum(sales_order.postage*sales_order.postage_currency_rate) as postage_amount_cny, currency.code from sales_order left join currency on currency.id=sales_order.postage_currency_id where ' . $where . ' and sales_order.postage_currency_id>0 group by sales_order.postage_currency_id')->fetchAll();
				$this->_view['postage'] = $postage;
				$this->_view['currency'] = Currency::find()->asArray()->getAll();
			}
		}
	}
	
	function actionShippingFeeSave()
	{
		$this->_view['title'] = '物流费用（保存）';
		$order_id = $_REQUEST['order_id'];
		if (isset($_REQUEST['shipping_fee']))
		{
			$shipping_fee = $_REQUEST['shipping_fee'];
			QDB::getConn()->execute('update sales_order set postage='.$shipping_fee.' where id='.$order_id);
			echo 1;exit;
		}
		if (isset($_REQUEST['postage_currency_id']))
		{
			$postage_currency_id = $_REQUEST['postage_currency_id'];
			$currency = Currency::find('id=?', $postage_currency_id)->asArray()->getOne();
			QDB::getConn()->execute('update sales_order set postage_currency_id=' . $currency['id'] . ',postage_currency_rate=' . $currency['rate'] . ' where id=' . $order_id);
			echo $currency['symbol'];exit;
		}
	}
	
	function actionExportPurchaseOrderInfo()
	{
		$this->_view['title'] = '请求下载采购订单信息表';
		set_time_limit(300);
		$url_arr = json_decode($_REQUEST['url_arr_json'], true);
		if ( ! empty($url_arr))
		{
			$result = Purchase_Order::search($url_arr, 0, 0);
			include Q::ini('custom_system/lib_dir').'PHPExcel.php';
			$phpexcel = new PHPExcel();
			$phpexcel->getProperties()->setCreator(Q::ini('custom_system/system_name') . Q::ini('custom_system/version'));
			$phpexcel->getProperties()->setLastModifiedBy(Q::ini('custom_system/system_name') . Q::ini('custom_system/version'));
			$phpexcel->getProperties()->setTitle('采购单信息表');
			$phpexcel->getProperties()->setSubject('采购单信息表');
			$phpexcel->getProperties()->setDescription('采购单信息表-运营支撑系统');
			$phpexcel->getProperties()->setKeywords('采购单信息表');
			$phpexcel->getProperties()->setCategory('采购单信息表');
			$phpexcel->setActiveSheetIndex(0);
			$phpexcel->getActiveSheet()->setTitle('采购单信息表');
			$phpexcel->getActiveSheet()->setCellValue('A1', '单号')->getColumnDimension('A')->setAutoSize(true);
			$phpexcel->getActiveSheet()->setCellValue('B1', '仓库')->getColumnDimension('B')->setAutoSize(true);
			$phpexcel->getActiveSheet()->setCellValue('C1', '采购人')->getColumnDimension('C')->setAutoSize(true);
			$phpexcel->getActiveSheet()->setCellValue('D1', '采购时间')->getColumnDimension('D')->setAutoSize(true);
			$phpexcel->getActiveSheet()->setCellValue('E1', '入库时间')->getColumnDimension('E')->setAutoSize(true);
			$phpexcel->getActiveSheet()->setCellValue('F1', '金额')->getColumnDimension('F')->setAutoSize(true);
			$phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('E1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('F1')->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$phpexcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$i = 2;
			foreach ($result['data'] as $r)
			{
				$phpexcel->getActiveSheet()->setCellValue('A'.$i, $r['number']);
				$phpexcel->getActiveSheet()->setCellValue('B'.$i, $r['warehouse_name']);
				$phpexcel->getActiveSheet()->setCellValue('C'.$i, $r['purchase_user_name']);
				$phpexcel->getActiveSheet()->setCellValue('D'.$i, $r['purchase_time']);
				$phpexcel->getActiveSheet()->setCellValue('E'.$i, $r['storage_audit_time']);
				$phpexcel->getActiveSheet()->setCellValue('F'.$i, $r['actual_amount']);
				$i++;
			}
			$outputFileName = "output.xls";
			$objWriter = new PHPExcel_Writer_Excel5($phpexcel);
			header("Content-Type: application/vnd.ms-execl");
			header("Content-Type: application/octet-stream");
			header("Content-Type: application/download");
			header('Content-Disposition:inline;filename="'.$outputFileName.'"');
			header("Content-Transfer-Encoding: binary");
			header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
			header("Pragma: no-cache");
			$objWriter->save('php://output');
			exit;
		}
	}
	
	function actionProductIO()
	{
		$this->_view['title'] = '产品收发查询';
		$warehouse = Warehouse::find()->asArray()->getAll();
		$this->_view['warehouse'] = Warehouse::find()->asArray()->getAll();
		if (isset($_GET['is_search']) && isset($_GET['product_number']))
		{
			$condition = Helper_BSS_Normal::buildCondition();
			$warehouse_ids = '';
			$where = '1';
			if ( ! empty($condition['begin_time']))
			{
				$where .= ' and inventory_log.time>=\'' . $condition['begin_time'] . '\'';
			}
			if ( ! empty($condition['end_time']))
			{
				$where .= ' and inventory_log.time<=\'' . $condition['end_time'] . ' 23:59:59\'';
			}
			if ( ! empty($condition['warehouse_id']))
			{
				$warehouse_ids = implode('\',\'', $condition['warehouse_id']);
			}
			$product_basic = Product_Basic::find('number=?', $condition['product_number'])->asArray()->getOne();
			if ($product_basic)
			{
				$product_warehouse = Product_Warehouse::find("product_basic_id=? and warehouse_id in ('{$warehouse_ids}')", $product_basic['id'])
					->joinLeft('warehouse', 'name as warehouse_name', 'warehouse.id=product_warehouse.warehouse_id')
					->asArray()->getOne();
				if ($product_warehouse)
				{
					$log = Inventory_Log::find("product_basic_id=? and warehouse_id in ('{$warehouse_ids}') and " . $where, $product_basic['id'])
						->joinLeft('user', 'name as user_name', 'user.id=inventory_log.user_id')
						->joinLeft('warehouse', 'name as warehouse_name', 'warehouse.id=inventory_log.warehouse_id')
						->asArray()->getAll();
					$this->_view['product_basic'] = $product_basic;
					$this->_view['product_warehouse'] = $product_warehouse;
					$this->_view['log'] = $log;
				}
			}
		}
	}
	
	function actionProvider()
	{
		$this->_view['title'] = '供应商请求ajax';
		$q = $_REQUEST['q'];
		$provider = Product_Provider::find('id like ? or name like ?', $q.'%', '%'.$q.'%')->asArray()->getAll();
		$result = array();
		foreach ($provider as $p)
		{
			$result[] = array(
				0 => $p['id'] . ' ' . $p['name'],
				'id' => $p['id']
			);
		}
		echo json_encode($result);
		exit;
	}
	
	function actionWarehouseAllocateList()
	{
		$this->_view['title'] = '仓库调拨单列表';
		$warehouse = Warehouse::find()->asArray()->getAll();
		$this->_view['warehouse'] = $warehouse;
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 0;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = Warehouse_Allocate::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$warehouse_allocate = $result['data'];
			foreach ($warehouse_allocate as &$wa)
			{
				$cost_amount = QDB::getConn()->execute('select sum(price*quantity) from warehouse_allocate_item where warehouse_allocate_id=' . $wa['id'])->fetchOne();
				$wa['cost_amount'] = $cost_amount;
			}
			unset($wa);
			$where = Warehouse_Allocate::buildSearchWhere($url_arr);
			$amount = QDB::getConn()->execute('select sum(price*quantity) from warehouse_allocate_item left join warehouse_allocate on warehouse_allocate.id=warehouse_allocate_item.warehouse_allocate_id where ' . $where)->fetchOne();
			$this->_view['amount'] = $amount;
			$page = new Helper_BSS_Page($result['pagination'], url('Finance::Business/WarehouseAllocateList', $url_arr));
			$this->_view['warehouse_allocate'] = $warehouse_allocate;
			$this->_view['page'] = $page->getPage();
		}
	}
	
	function actionWarehouseAllocateAudit()
	{
		$this->_view['title'] = '审核调拨单';
		$ids = $_REQUEST['ids'];
		if ($ids)
		{
			$ids = explode(',', $ids);
			$result = Warehouse_Allocate::financeAudit($ids);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('审核成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('审核失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionWarehouseAllocateDetail()
	{
		$this->_view['title'] = '调拨单详细';
		if ( ! empty($_GET['id']))
		{
			$columns = array(
				'warehouse_allocate.*',
				'a.name as start_warehouse_name',
				'b.name as end_warehouse_name',
				'c.name as make_user_name'
			);
			$warehouse_allocate = Warehouse_Allocate::find('warehouse_allocate.id=?', $_GET['id'])
				->joinLeft('warehouse as a', '', 'a.id=warehouse_allocate.start_warehouse_id')
				->joinLeft('warehouse as b', '', 'b.id=warehouse_allocate.end_warehouse_id')
				->joinLeft('user as c', '', 'c.id=warehouse_allocate.make_user_id')
				->setColumns($columns)->asArray()->getOne();
			$item = Warehouse_Allocate_Item::find('warehouse_allocate_item.warehouse_allocate_id=?', $_GET['id'])
				->joinLeft('product_basic', 'product_basic.name as product_name, product_basic.number as number', 'product_basic.id=warehouse_allocate_item.product_basic_id')
				->asArray()->getAll();
			Helper_BSS_Normal::getProductImgUrl($item);
			$warehouse_allocate['item'] = $item;
			$this->_view['warehouse_allocate'] = $warehouse_allocate;
		}
		else
		{
			return $this->_redirectMessage('', '提交方式有误', url('Storage::Allocate/List'), FAILURE);
		}
	}
	
	function actionOverseasSendList()
	{
		$this->_view['title'] = "海外仓发送单列表";
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$this->_view['warehouse'] = Warehouse::getActiveWarehouse();
		$condition = Helper_BSS_Normal::buildCondition();
		$search_result = Overseas_Send::search($condition, $cp, $ps, 'overseas_send.id desc');
		Overseas_Send::getPurchaseOrderInfo($search_result['data']);
		$page = new Helper_BSS_Page($search_result['pagination'], url('Storage::Overseas/SendList', $condition));
		$this->_view['page'] = $page->getPage();
		$this->_view['send_order'] = $search_result['data'];
	}
	
	function actionOverseasSendEdit()
	{
		$this->_view['title'] = "海外仓发送单编辑";
		$result = Overseas_Send::find('overseas_send.id=?', $_GET['id'])
			->joinLeft('warehouse', '', 'overseas_send.warehouse_id=warehouse.id')
			->columns('warehouse.name as warehouse_name')
			->asArray()->getOne();
		$this->_view['send_order'] = $result;
		$purchase_order_number = array();
		$purchase_order = Overseas_Send_Item::find('overseas_send_id=?', $_GET['id'])
			->joinLeft('purchase_order', 'purchase_order.number', 'purchase_order.id=overseas_send_item.purchase_order_id')
			->asArray()->getAll();
		$this->_view['purchase_order'] = $purchase_order;
	}
	
	function actionOverseasSendEditSave()
	{
		$this->_view['title'] = "海外仓发送单编辑（保存）";
		if ($this->_context->isPOST())
		{
			if ($_POST['id'])
			{
				$order = array(
					'id' => $_POST['id'],
					'shipping_fee' => $_POST['shipping_fee'],
					'tax_fee' => $_POST['tax_fee']
				);
				$result = Overseas_Send::financeEdit($order);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('保存成功', '正在返回..', url('Finance::Business/OverseasSendList'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('保存失败', $result['message'], url('Finance::Business/OverseasSendList'), $result['ack']);
				}
			}
		}
	}
	
	function actionOverseasSendAudit()
	{
		$this->_view['title'] = "海外仓发送单审核";
		$ids = $_REQUEST['ids'];
		if ($ids)
		{
			$ids = explode(',', $ids);
			$result = Overseas_Send::financeAudit($ids);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('审核成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('审核失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionShippingFeeExport()
	{
		$this->_view['title'] = '运费导出';
		$url_arr = json_decode($_REQUEST['url_arr'], 1);
		$search_result = Sales_Order::search($url_arr, 0, 0);
		if (count($search_result['data']) <= 50000)
		{
			Helper_BSS_XLS::ShippingFeeExport($search_result['data']);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '导出记录超过五万条..', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
}